package org.itboys.core.controller.front.wallet;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.core.service.mysql.front.SundayFrontWalletService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.MybatisParamUtil;
import org.itboys.core.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/11/27
 * @Description：钱包
 */
@Controller
@RequestMapping("/sunday/mobile/wallet")
@Api(value = "wallet", description = "钱包")
public class SundayFrontWalletController {

    @Autowired
    private SundayFrontWalletService walletService;

    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @ApiOperation(value = "申请提现")
    public void apply(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "收款账号", required = true) @RequestParam(value = "account") String account,
            @ApiParam(value = "金额", required = true) @RequestParam(value = "money") BigDecimal money

    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        walletService.createTXRecord(memberId, money, account);
        ResponseMessage.success();
    }

    @RequestMapping(value = "/createRecord", method = RequestMethod.POST)
    @ApiOperation(value = "创建充值记录")
    public void createRecord(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "充值金额", required = true) @RequestParam(value = "amount") BigDecimal amount
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        Map<String, Object> result = walletService.createCZRecord(memberId, amount);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/balance", method = RequestMethod.POST)
    @ApiOperation(value = "【租赁端】余额/【供应商】收入")
    public void balance(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "时间格式：2018-12-10") @RequestParam(value = "ct", required = false) String ct
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        try {
            ResponseMessage.success(walletService.getBalance(memberId, ct));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/record", method = RequestMethod.POST)
    @ApiOperation("【租赁端】明细")
    public void record(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "时间格式：2018-12-10") @RequestParam(value = "ct", required = false) String ct
            , @ApiParam(value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = false) String pageSize
            , @ApiParam(value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = false) String pageNumber
            , @ApiParam(value = "排序参数。字段名称。默认为空。") @RequestParam(value = "sort", required = false) String sort
            , @ApiParam(value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order) {
        MybatisParamUtil paramUtil = new MybatisParamUtil(request);
         Long memberId = AccountIdHoldUtils.getMemberId();

        ResponseMessage.success(walletService.record(ct, memberId, paramUtil));
    }
}
